package com.gyh.stlserver.controller;

import cn.hutool.core.util.ObjectUtil;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.entity.Course;
import com.gyh.stlserver.entity.Member;
import com.gyh.stlserver.entity.Msg;
import com.gyh.stlserver.entity.Reserve;
import com.gyh.stlserver.mapper.ReserveMapper;
import com.gyh.stlserver.mapper.RoomMapper;
import com.gyh.stlserver.service.CourseService;
import com.gyh.stlserver.service.MemberService;
import com.gyh.stlserver.service.MsgService;
import com.gyh.stlserver.service.ReserveService;
import com.gyh.stlserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ReserveMapper reserveMapper;
    @Resource
    private RoomMapper roomMapper;
    @GetMapping
    public Result search(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer pageSize, Course course){
        return Result.success(Constants.STATUS_200,courseService.getAllByCourseVo(pageNum,pageSize,course));
    }
    @GetMapping("/getAll")
    public Result getAll(){
        return Result.success(Constants.STATUS_200,courseService.getAllCourseVo());
    }
    @GetMapping("/getAllCourse")
    public Result getAllCourse(){
        return Result.success(Constants.STATUS_200,courseService.getAllCourse());
    }
    /*添加课程*/
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        int i = courseService.addCourse(course);
        if (i==-1) {
            String roomName = roomMapper.selectNameById(course.getRoomId());
            return Result.error(Constants.STATUS_401,"教室"+roomName+"已被占用");
        }
        if (i==-2){
            return Result.error(Constants.STATUS_401,"教练没有时间");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*编辑课程*/
    @PutMapping
    public Result editCourse(@RequestBody Course course){
        int i = courseService.editCourse(course);
        if (i==-1){
            return Result.error(Constants.STATUS_401,"改时间段教室被占用");
        } else if (i == -2) {
            return Result.error(Constants.STATUS_401,"教练该时间段有课");
        }
        return Result.success(200);
    }
    @DeleteMapping("{id}")
    public Result remove(@PathVariable("id") Integer id){
        Course course = courseService.getCourseById(id);
        if (ObjectUtil.isEmpty(course)){
            return Result.error(Constants.STATUS_401,"无法删除不存在的课程");
        }
        Reserve reserve = reserveMapper.selectReserveByCidAndStatus(id);
        if (ObjectUtil.isNotEmpty(reserve)){
            return Result.error(Constants.STATUS_401,"课程有预约无法删除");
        }
        int i = courseService.remove(id);
        if (i<1){
            return Result.error(Constants.STATUS_401,"删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }

}
